package com.pxmeta.app.controller.user;


import com.google.common.collect.Lists;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.user.entity.Rank;
import com.pxmeta.user.entity.UserRank;
import com.pxmeta.user.entity.vo.RankVO;
import com.pxmeta.user.service.IRankService;
import com.pxmeta.user.service.IUserRankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Api(value = "等级模块",tags = "等级模块")
@RequestMapping(value = "/rank")
@CrossOrigin
@RequiredArgsConstructor
public class RankController {

    private final IRankService rankService;

    private final IUserRankService userRankService;

    @GetMapping("/getRanks")
    @ApiOperation("获取等级列表")
    public AjaxResult getRanks(@RequestParam Integer type){
        List<Rank> list = rankService.getRankList(type);
        Long userId = SecurityUtils.getUserId();
        UserRank userRank = userRankService.getById(userId);
        List<RankVO> finalList = Lists.newLinkedList();
        for (Rank rank : list) {
            RankVO vo = new RankVO();
            vo.setExp(rank.getExp());
            vo.setId(rank.getId());

            if (userRank == null){
                vo.setCurrentExp(0);
            }else {
                if (type == 1){
                    vo.setCurrentExp(userRank.getNobleRank());
                }else {
                    vo.setCurrentExp(userRank.getCharmRank());
                }
            }
            vo.setName(rank.getName());
            String powerIdxs = rank.getPowerIdxs();
            List<String> powerList = Arrays.asList(powerIdxs.split(","));
            vo.setPowerIdList(powerList);

            finalList.add(vo);
        }
        return AjaxResult.success(finalList);
    }
}
